#include "tcp_socket.hpp"

int main(int argc, char *argv[])
{
    if (argc != 3) {
        std::cout<<"please input server address!\n";
        std::cout<<"usage: ./tcp_srv 192.168.197.128 3090\n";
        return -1;
    }
    std::string srv_ip = argv[1];
    uint16_t srv_port = std::stoi(argv[2]);
    TcpSocket cli_sock;
    //1. 创建套接字
    CHECK_RET(cli_sock.Socket());
    //2. 绑定地址信息-不推荐
    //3. 向服务端发起连接请求
    CHECK_RET(cli_sock.Connect(srv_ip, srv_port));
    while(1) {
        //4. 与服务端进行通信
        std::string buf;
        std::cout << "client say:";
        fflush(stdout);
        std::cin >> buf;
        bool ret =cli_sock.Send(buf);
        if(ret == false){
          cli_sock.Close();
          return -1;
        }

        buf.clear();
        ret = cli_sock.Recv(&buf);
        if(ret == false){
          cli_sock.Close();
          return -1;
        }
        std::cout << "server say:" << buf << std::endl;
    }
    //5. 关闭套接字
    cli_sock.Close();
    return 0;
}

